Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace outputstream with writeroutputstream #234

Merged
merged 7 commits into from
Jul 17, 2021

Conversation

doniwinata0309
Copy link

@doniwinata0309 doniwinata0309 commented Nov 2, 2020

Issue:
#233

  • use WriterOutputStream from apache common

@doniwinata0309 doniwinata0309 changed the title Replace output stream with writeroutputstream Replace outputstream with writeroutputstream Nov 2, 2020
OutputStream os = resource.openOutputStream();
openStreams.add(os);

OutputStream os = new WriterOutputStream(resource.openWriter());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constructor is depreciated, we should define a charset.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to
OutputStream os = new WriterOutputStream(sourceFile.openWriter(), "UTF-8");

is it okay to use UTF-8 ?

for (OutputStream openStream : openStreams) {
openStream.flush();
openStream.close();
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not keep these calls? It seems like they would be necessary because we'll be dealing with a buffer with WriterOutputStream.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is causing this error

Caused by: java.io.IOException: Stream closed
    at sun.nio.cs.StreamEncoder.ensureOpen (StreamEncoder.java:45)
    at sun.nio.cs.StreamEncoder.flush (StreamEncoder.java:140)
    at java.io.OutputStreamWriter.flush (OutputStreamWriter.java:229)
    at java.io.FilterWriter.flush (FilterWriter.java:100)
    at org.androidtransfuse.util.apache.commons.WriterOutputStream.flush (WriterOutputStream.java:83)
    at org.androidtransfuse.gen.FilerSourceCodeWriter.close (FilerSourceCodeWriter.java:65)
    at com.sun.codemodel.JCodeModel.build (JCodeModel.java:312)

Seems it already closed by the WriterOutputStream

    public void close() throws IOException {
        this.processInput(true);
        this.flushOutput();
        this.writer.close();
    }

Do you have other clue why it causing error ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I bet codemodel is already closing the output stream and since it's closed flush throws an error. You can outright get rid of the flush call instead of just commenting it out.

Copy link
Author

@doniwinata0309 doniwinata0309 Nov 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it seems already flush and also closed from the log, also looking from the flamegraph.


codeWriter.close();
verify(mockOutputStream).flush();
verify(mockOutputStream).close();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I'd really like to keep these tests.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now it seems this test not doing anything right ? do you have idea what we should test, or should we remove it ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would breing everything back except for verify(mockOutputStream).flush();

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just modify the test, and add spy there when creating codeWriter object. Also, i put a new method so we can mock this part
doReturn(mockOutputStream).when(codeWriter).getWriterOutputStream(mockWriter);
please let me know if you have better approach on this.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, great, I think that works.

@@ -53,8 +53,9 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<version>2.5</version>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍... there are a few more commons-io used in testing, could you upgrade those as well in this pass?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -49,10 +50,8 @@ public OutputStream openBinary(JPackage jPackage, String fileName) throws IOExce
//generate a source file based on package and fileName
String qualified = toQualifiedClassName(jPackage, fileName);
JavaFileObject sourceFile = filer.createSourceFile(qualified, originating.getOriginatingElements(qualified));

OutputStream os = sourceFile.openOutputStream();
OutputStream os = new WriterOutputStream(sourceFile.openWriter(), "UTF-8");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass in StandardCharsets.UTF_8 instead of the string?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes, i just replace it

@@ -63,7 +62,7 @@ private String toQualifiedClassName(JPackage pkg, String fileName) {
@Override
public void close() throws IOException {
for (OutputStream openStream : openStreams) {
openStream.flush();
// openStream.flush();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}
}
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go ahead and get rid of anything not used. Since we're using utf-8, the checkIbmJdkWithBrokenUTF16 will never execute, and most of the constructors can be combined into one.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, i just removed it and combine the constructor.

…fuse-support, remove unused code on writeroutputstream
@@ -0,0 +1,88 @@
package org.androidtransfuse.util.apache.commons;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd stick this in org.androidtransfuse.gen next to FilerSourceCodeWriter

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, just moved next to FilerSourceCodeWriter

openStreams.add(os);

return os;
}

public OutputStream getWriterOutputStream(Writer writer) {
return new WriterOutputStream(writer, StandardCharsets.UTF_8);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@johncarl81 using StandardCharsets.UTF_8 this actually red on my editor. it required
1.6
1.6
to use 1.7. However it compiled successfully. Should we just leave it, or update it to 1.7 ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, too big of a chance for this... maybe just define the string as a constant in this class?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes i changed it to Charset.forName("UTF-8") it should be equals to it.

*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Revert these.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@doniwinata0309
Copy link
Author

Hi do you still have any further concern about this PR ? thank you

@johncarl81
Copy link
Owner

Nope, I'm ready to merge. I just haven't had a moment to focus on this. Do you need this and parceler updated and pushed out to maven central?

@doniwinata0309
Copy link
Author

thanks @johncarl81, yes would be nice if i can try it on stable version. But actually not that urgent, i guess it is up to you.

@johncarl81 johncarl81 merged commit 217e00f into johncarl81:master Jul 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants